import 'package:flutter/material.dart';
import 'package:imooc_flutter_2024/dao/login_dao.dart';
import 'package:imooc_flutter_2024/util/navigator_utils.dart';
import 'package:imooc_flutter_2024/util/string_utils.dart';
import 'package:imooc_flutter_2024/util/view_utils.dart';
import 'package:imooc_flutter_2024/widget/input_widget.dart';
import 'package:imooc_flutter_2024/widget/login_widget.dart';
import 'package:url_launcher/url_launcher.dart';

class LoginPage extends StatefulWidget {
  const LoginPage({super.key});

  @override
  State<LoginPage> createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  bool loginEnabled = false;
  String? username;
  String? password;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: [
          // Text('hello world')
          ..._background(),
          _content(),
        ],
      ),
    );
  }

  List<Widget> _background() {
    return [
      Positioned.fill(
          child: Image.asset('images/login-bg1.jpg', fit: BoxFit.cover)),
      Positioned.fill(child: Container(color: Colors.black54))
    ];
  }

  void _checkInput() {
    bool enabled = isNotEmpty(username) && isNotEmpty(password);
    setState(() {
      loginEnabled = enabled;
    });
  }

  void _login(context) {
    var result = LoginDao.login(username: username!, password: password!);
    NavigatorUtils.gotoHome(context);
  }

  void _gotoRegister() async {
    Uri url = Uri.parse('https://m.baidu.com');
    if (!await launchUrl(url, mode: LaunchMode.externalApplication)) {
      throw 'Could not launch $url';
    }
  }

  _content() {
    return Positioned.fill(
        left: 25,
        right: 25,
        child: ListView(
          children: [
            hiSpace(height: 100),
            const Text(
              'Login Page',
              style: TextStyle(
                  fontSize: 30,
                  fontWeight: FontWeight.bold,
                  color: Colors.white),
            ),
            hiSpace(height: 100),
            InputWidget(
              "input_username",
              onChanged: (value) {
                username = value;
                _checkInput();
              },
            ),
            hiSpace(height: 10),
            InputWidget('input_password', obscureText: true,
                onChanged: (value) {
              password = value;
              _checkInput();
            }),
            hiSpace(height: 30),
            LoginButton(
              'login',
              onPressed: () => _login(context),
              enabled: loginEnabled,
            ),
            hiSpace(height: 10),
            Container(
              alignment: Alignment.centerRight,
              child: InkWell(
                onTap: _gotoRegister,
                child: Text(
                  'Forgot Password?',
                  style: TextStyle(color: Colors.white),
                ),
              ),
            )
          ],
        ));
  }
}
